const app = getApp();
var QQMapWX = require('../../resource/js/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data: {
    style:'icon-roundcheckfill icon_roundcheckfill',
    styles:'icon-round icon_round',
    sex_type:true,
    address:'点击选择',
    submit:'添加收货地址',
    name: '',
    addressed:'',
    phone:'',
    id:'',
    latitude:'',
    longitude:''
  },

  onLoad: function(options){
    const that = this;
    if(options.id){
      app.util.request({
        url: 'Index/Index/editAddress',
        data: {
          openid: app.globalData.userInfo.openid,
          id: options.id
        },
        method: 'post',
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          if (res.data.sex === 1) {
            that.setData({
              style: 'icon-roundcheckfill icon_roundcheckfill',
              styles: 'icon-round icon_round',
              sex_type: true
            })
          } else {
            that.setData({
              styles: 'icon-roundcheckfill icon_roundcheckfill',
              style: 'icon-round icon_round',
              sex_type: false
            })
          }
          that.setData({
            name: res.data.name,
            address: res.data.address,
            addressed: res.data.addressed,
            id: options.id,
            submit:'修改收货地址',
            phone: res.data.phone
          })
        }
      })
    }
  },

  sex:function(e){
    const that = this;
    if(e.target.dataset.style === that.data.styles){
      that.setData({
        styles:'icon-roundcheckfill icon_roundcheckfill',
        style:'icon-round icon_round',
        sex_type: false,
      })
    }else{
      that.setData({
        style: 'icon-roundcheckfill icon_roundcheckfill',
        styles: 'icon-round icon_round',
        sex_type: true,
      })
    }
  },

  map:function(e){
    wx.navigateTo({
      url: '../map/map'
    })
  },

  setName:function(e){
    const that = this;
    that.setData({
      name:e.detail.value
    })
  },

  setAddress:function(e){
    const that = this;
    that.setData({
      addressed:e.detail.value
    })
  },

  setPhone:function(e){
    const that = this;
    that.setData({
      phone:e.detail.value
    })
  },

  formSubmitAddress:function(e){
    const that = this;
    var sex = 1;
    if (that.data.name === ''){
      wx.showToast({
        title: '联系人姓名不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (that.data.address === '点击选择'){
      wx.showToast({
        title: '收货地址不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (that.data.addressed === ''){
      wx.showToast({
        title: '详细地址不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    const phone = that.data.phone;
    const req = /^1[34578]\d{9}$/;
    if (!(req.test(phone))){
      wx.showToast({
        title: '手机号格式不正确请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if(that.data.sex_type){
      sex = 1;
    }else{
      sex = 0;
    }

    if (that.data.submit === '修改收货地址'){
      app.util.request({
        url: 'Index/Index/ModifyData',
        data: {
          id: that.data.id,
          openid: app.globalData.userInfo.openid,
          name: that.data.name,
          sex: sex,
          phone: that.data.phone,
          address: that.data.address,
          addressed: that.data.addressed,
          latitude: that.data.latitude,
          longitude: that.data.longitude
        },
        method: 'post',
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          if (res.data.code === 0) {
            wx.showToast({
              title: '更新成功',
              icon: 'success',
              duration: 2000
            })
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
    }else{
      app.util.request({
        url: 'Index/Index/addAddress',
        data: {
          openid: app.globalData.userInfo.openid,
          name: that.data.name,
          sex: sex,
          phone: that.data.phone,
          address: that.data.address,
          addressed: that.data.addressed,
          latitude: that.data.latitude,
          longitude: that.data.longitude
        },
        method: 'post',
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          if (res.data.code === 0) {
            wx.showToast({
              title: '添加成功',
              icon: 'success',
              duration: 2000
            })
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
    }
  },
  onShow: function () {
    const that = this;
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if(currPage.data.localhost){
      const data = currPage.data.localhost;
      if (data.address == ''){
        qqmapsdk = new QQMapWX({ key: 'KJHBZ-PMX6X-MXE44-TRKVY-LC6YQ-JYBTM' });
        qqmapsdk.reverseGeocoder({
          location: data.latitude + ',' + data.longitude,
          success: function (res) {
            that.setData({
              address: res.result.address,
              latitude: data.latitude,
              longitude: data.longitude
            })
          }
        })
      }else{
        that.setData({
          address: data.address,
          latitude: data.latitude,
          longitude: data.longitude
        })
      }
    }
  }
})